package com.cybertron.ironhide.manage.action;

import java.util.List;

import javax.annotation.Resource;

import com.cybertron.ironhide.manage.common.base.BaseAction;
import com.cybertron.ironhide.manage.domain.Article;
import com.cybertron.ironhide.manage.domain.Comment;
import com.cybertron.ironhide.manage.service.ArticleService;
import com.cybertron.ironhide.manage.service.CommentService;

public class ArticleAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2907635285026839852L;

	@Resource
	private ArticleService articleService;

	@Resource
	private CommentService commentService;

	private List<Article> articles;

	private List<Comment> comments;

	private Article article;

	private Comment comment;

	private String[] articleIds;

	public String list() throws Exception {
		articles = articleService.selectBySelective(article, pager);
		return LIST;
	}

	public String add() {
		return ADD;
	}

	public String doAdd() {
		articleService.insertSelective(article);
		return ajaxSuccess("添加成功", "article-list", null, CLOSE_CURRENT);
	}

	public String edit() {
		article = articleService.selectByPrimaryKey(article.getId());
		return EDIT;
	}

	public String doEdit() {
		articleService.updateByPrimaryKeySelective(article);
		return ajaxSuccess("修改成功", "article-list", null, CLOSE_CURRENT);
	}

	public String delete() {
		articleService.deleteByPrimaryKey(article.getId());
		return ajaxSuccess("删除成功", "article-list");

	}

	public String deletes() {
		articleService.deleteByPrimarykeys(articleIds);
		return ajaxSuccess("删除成功", "article-list");
	}

	public String commentList() {
		comments = commentService.selectByArticleId(article.getId());
		return "comment_list";
	}

	public String commentDelete() {
		commentService.deleteByPrimaryKey(comment.getId());
		return ajaxSuccess("删除成功", "comment-list");
	}

	public String checkTitle() {
		boolean isValid = articleService.checkTitle(article.getTitle());
		if (isValid) {
			return ajaxText("false");
		} else {
			return ajaxText("true");
		}
	}

	public ArticleService getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public List<Article> getArticles() {
		return articles;
	}

	public void setArticles(List<Article> articles) {
		this.articles = articles;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Comment getComment() {
		return comment;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}

	public String[] getArticleIds() {
		return articleIds;
	}

	public void setArticleIds(String[] articleIds) {
		this.articleIds = articleIds;
	}

	public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

}
